#if !defined(AFX_ORDERSTATEGRID_H__10100D25_DE26_4FF4_B708_B47C3CA12682__INCLUDED_)
#define AFX_ORDERSTATEGRID_H__10100D25_DE26_4FF4_B708_B47C3CA12682__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// OrderStateGrid.h : header file
//
#include "UGCtrl.h"
#include "resource.h"

class ArrowCellType : public CUGCellType
{
protected:
	CBitmap	m_bmpUp;
	CBitmap	m_bmpDown;

public:
	ArrowCellType()
	{
		m_bmpUp.LoadBitmap(IDB_BITMAP_UP);
		m_bmpDown.LoadBitmap(IDB_BITMAP_DOWN);
	}

	~ArrowCellType()
	{
		m_bmpUp.DeleteObject();
		m_bmpDown.DeleteObject();
	}

	virtual void OnDraw(CDC *dc, RECT *rect, int col, long row, CUGCell *cell, int selected, int current)
	{
		// Call base class drawing
		CUGCellType::OnDraw(dc, rect, col, row, cell, selected, current);

		// Draw arrow bitmap
		CBitmap *pBmp = (cell->GetParam()) ? &m_bmpDown : &m_bmpUp;
		DrawTransparentBitmap(dc->m_hDC, (HBITMAP) *pBmp, (short) (rect->left + 1), (short) (rect->top + ((rect->bottom - rect->top) - 8) / 2), RGB(0, 0, 0));
	}

	void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart,
		short yStart, COLORREF cTransparentColor)
	{
		BITMAP     bm;
		COLORREF   cColor;
		HBITMAP    bmAndBack, bmAndObject, bmAndMem, bmSave;
		HBITMAP    bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
		HDC        hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
		POINT      ptSize;

		hdcTemp = CreateCompatibleDC(hdc);
		SelectObject(hdcTemp, hBitmap);   // Select the bitmap

		GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &bm);
		ptSize.x = bm.bmWidth;            // Get width of bitmap
		ptSize.y = bm.bmHeight;           // Get height of bitmap
		DPtoLP(hdcTemp, &ptSize, 1);      // Convert from device

		// to logical points

		// Create some DCs to hold temporary data.
		hdcBack = CreateCompatibleDC(hdc);
		hdcObject = CreateCompatibleDC(hdc);
		hdcMem = CreateCompatibleDC(hdc);
		hdcSave = CreateCompatibleDC(hdc);

		// Create a bitmap for each DC. DCs are required for a number of
		// GDI functions.

		// Monochrome DC
		bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

		// Monochrome DC
		bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);

		bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
		bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);

		// Each DC must select a bitmap object to store pixel data.
		bmBackOld = (HBITMAP) SelectObject(hdcBack, bmAndBack);
		bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
		bmMemOld = (HBITMAP) SelectObject(hdcMem, bmAndMem);
		bmSaveOld = (HBITMAP) SelectObject(hdcSave, bmSave);

		// Set proper mapping mode.
		SetMapMode(hdcTemp, GetMapMode(hdc));

		// Save the bitmap sent here, because it will be overwritten.
		BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);

		// Set the background color of the source DC to the color.
		// contained in the parts of the bitmap that should be transparent
		cColor = SetBkColor(hdcTemp, cTransparentColor);

		// Create the object mask for the bitmap by performing a BitBlt
		// from the source bitmap to a monochrome bitmap.
		BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
			SRCCOPY);

		// Set the background color of the source DC back to the original
		// color.
		SetBkColor(hdcTemp, cColor);

		// Create the inverse of the object mask.
		BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
			NOTSRCCOPY);

		// Copy the background of the main DC to the destination.
		BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
			SRCCOPY);

		// Mask out the places where the bitmap will be placed.
		BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);

		// Mask out the transparent colored pixels on the bitmap.
		BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);

		// XOR the bitmap with the background on the destination DC.
		BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);

		// Copy the destination to the screen.
		BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
			SRCCOPY);

		// Place the original bitmap back into the bitmap sent here.
		BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

		// Delete the memory bitmaps.
		DeleteObject(SelectObject(hdcBack, bmBackOld));
		DeleteObject(SelectObject(hdcObject, bmObjectOld));
		DeleteObject(SelectObject(hdcMem, bmMemOld));
		DeleteObject(SelectObject(hdcSave, bmSaveOld));

		// Delete the memory DCs.
		DeleteDC(hdcMem);
		DeleteDC(hdcBack);
		DeleteDC(hdcObject);
		DeleteDC(hdcSave);
		DeleteDC(hdcTemp);
	}
};


/////////////////////////////////////////////////////////////////////////////
// COrderStateGrid window
class CMainOrderPanel;
class CAccountInfo;
class CPositionInfo;
class CAccountManager;
class COrderStateGrid : public CUGCtrl
{
// Construction
public:
	COrderStateGrid();
	ArrowCellType	m_celllArrow;
	int				m_nArrowCellIndx;

// Attributes
public:
	void SetAccountInfo(CAccountInfo* a_AccInfo);
	void SetPositionInfo(CPositionInfo* a_Posi);
	void ClearAccountInfo();
	void ClearPositionInfo();
	CMainOrderPanel* m_pMainWnd;

	virtual void OnMouseMove(int col, long row, POINT *point, UINT nFlags, BOOL processed = 0);
	virtual void OnLClicked(int col, long row, int updn, RECT *rect, POINT *point, int processed);
	//GetCellIndirect notification
	virtual void OnGetCell(int col, long row, CUGCell *cell);
	POINT m_Prev;
	BOOL m_bDrawing;
// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(COrderStateGrid)
	//***** Over-ridable Notify Functions *****
	virtual void OnSetup();
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~COrderStateGrid();

	CFont m_font1;
	CFont m_font2;

	// Generated message map functions
protected:
	//{{AFX_MSG(COrderStateGrid)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ORDERSTATEGRID_H__10100D25_DE26_4FF4_B708_B47C3CA12682__INCLUDED_)
